
from PyQt4.QtGui import QItemDelegate
from PyQt4.QtCore import SIGNAL, QString
from SimpleCells.Gui.SCArrayDialog import SCArrayDialog

try:
    from numpy import ndarray
except ImportError:
    ndarray = None
    
class SCListDelegate(QItemDelegate):
    
    def __init__(self,parent=None):
        QItemDelegate.__init__(self,parent)

    def createEditor(self,parent,option,index):
        if not index.isValid():
            return None
        value = index.model().getData().valueFromIndex(index.row())
        if isinstance(value,ndarray):
            dialog = SCArrayDialog(value,parent)
            dialog.sizeHint()
            return dialog
        else:
            key = index.model().getData().keyFromIndex(index.row())
            command = 'free["%s"] = %s' % (key,repr(value))
            self.emit(SIGNAL('putOnShell(QString)'),QString(command))

    def setModelData(self, editor, model, index):
        if isinstance(editor,SCArrayDialog):
            model.setData(index,editor.getData())
        else:
            QItemDelegate.setModelData(self,editor,model,index)
            